package com.elt.config;

import java.util.HashMap;

import com.elt.application.ELTApplicationInfo;
import com.elt.util.CheckUtil;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;

public class XmlConfig {

	private static HashMap<String, String> map = new HashMap<String, String>();

	public static void init(int xmlid) {
		try {
			XmlResourceParser parser = ELTApplicationInfo.appContext
					.getResources().getXml(xmlid);
			String key = "";
			String value = "";

			while (parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
				if (parser.getEventType() == XmlResourceParser.START_TAG) {
					if (parser.getName().endsWith("bean")) {
						key = parser.getAttributeValue(0);
						value = parser.getAttributeValue(1);
						map.put(key, value);
						Log.d("key *************", value);
					}
				}
				parser.next();
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.clear();
		}
	}

	public static <T> T getValue(String key) {
		String path = map.get(key);

		T t = null;
		if (!CheckUtil.isNotNullString(path)) {
			return t;
		}

		try {
			Class<T> clazz = (Class<T>) Class.forName(path);
			t = clazz.newInstance();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}

}
